package com.itheima.app.controller;

import cn.hutool.core.util.StrUtil;
import com.itheima.app.manager.VideoManager;
import com.itheima.domain.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {
    @Autowired
    private VideoManager videoManager;

    /**
     * 分页查询小视频列表
     *
     * @param page     页码
     * @param pagesize 每页条数
     * @return 视频分页对象
     */
    @GetMapping("/smallVideos")
    public PageBeanVo videoList(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        return videoManager.videoList(page, pagesize);
    }

    /**
     * 上传小视频
     *
     * @param videoThumbnail 视频封面图片
     * @param videoFile      视频文件
     */
    @PostMapping("/smallVideos")
    public void uploadVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videoManager.uploadVideo(videoThumbnail, videoFile);
    }

    /**
     * 关注用户
     *
     * @param videoUserId 视频发布者id
     */
    @PostMapping("/smallVideos/{videoUserId}/userFocus")
    public void focusOnUser(@PathVariable Long videoUserId) {
        videoManager.focusOnUser(videoUserId);
    }

    /**
     * 取消关注用户
     *
     * @param videoUserId 视频发布者id
     */
    @PostMapping("/smallVideos/{videoUserId}/userUnFocus")
    public void unFollowUser(@PathVariable Long videoUserId) {
        videoManager.unFollowUser(videoUserId);
    }

    /**
     * 视频点赞
     *
     * @param videoId 视频id
     * @return
     */
    @PostMapping("/smallVideos/{id}/like")
    public Map videoLike(
            @PathVariable("id") String videoId
    ) {
        return videoManager.videoLike(videoId);
    }

    /**
     * 视频取消点赞
     *
     * @param videoId 视频id
     * @return
     */

    @PostMapping("/smallVideos/{id}/dislike")
    public Map videoDislike(
            @PathVariable("id") String videoId
    ) {
        return videoManager.videoDislike(videoId);
    }

    /**
     * 发布视频评论
     *
     * @param videoId 视频id
     * @param map     返回集合为null
     * @return
     */
    @PostMapping("/smallVideos/{videoId}/comments")
    public Map saveVideoComments(
            @PathVariable String videoId,
            @RequestBody Map<String, String> map
    ) {
        String content = map.get("comment");
        videoManager.saveVideoComments(videoId, content);
        return null;
    }

    /**
     * 查询视频评论列表
     *
     * @param page
     * @param pageSize
     * @param videoId  视频id
     * @return
     */
    @GetMapping("/smallVideos/{id}/comments")
    public PageBeanVo findVideoComments(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @PathVariable("id") String videoId
    ) {
        return videoManager.findVideoComments(page, pageSize, videoId);
    }

    /**
     * 视频评论点赞
     *
     * @param publishId 视频评论id
     * @return
     */
    @PostMapping("/smallVideos/comments/{publishId}/like")
    public Map videoCommentLike(
            @PathVariable String publishId
    ) {
        videoManager.videoCommentLike(publishId);
        return null;
    }

    /**
     * 视频评论取消点赞
     *
     * @param publishId 视频评论id
     * @return
     */
    @PostMapping("/smallVideos/comments/{publishId}/dislike")
    public Map videoCommentDislike(
            @PathVariable String publishId
    ) {
        videoManager.videoCommentDislike(publishId);
        return null;
    }
}
